/**
 * 分享
 */







function loadQQApi(qq, callback = null) {
  if (!qq) { // qq == 0
    return callback && callback();
  }
  let script = document.createElement("script");
  script.onload = function() {
    typeof callback === "function" && callback()
  };
  script.onerror = function() {};
  script.src = qq === 1 ? 'http://3gimg.qq.com/html5/js/qb.js' : 'http://jsapi.qq.com/get?api=app.share';
  document.body.appendChild(script);
}

function ucSharePlatform(type, os) {
  let platform = '';
  if (os == 2) {
    if (type == 1) {
      platform = 'WechatTimeline';
    } else if (type == 2) {
      platform = 'WechatFriends';
    } else if (type == 3) {
      platform = 'QQ';
    } else if (type == 4) {
      platform = 'SinaWeibo';
    } else if (type == 0) {
      platform = 'undefined';
    }
  } else if (os == 1) {
    if (type == 1) {
      platform = 'kWeixinFriend';
    } else if (type == 2) {
      platform = 'kWeixin';
    } else if (type == 3) {
      platform = 'kQQ';
    } else if (type == 4) {
      platform = 'kSinaWeibo';
    } else if (type == 0) {
      platform = 'undefined';
    }
  }
  return platform;
}



const share = {
  /**
   * @param {Object} options = {
     title,
     url,
     desc,
     img,
     type // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接弹出原生 3==>QQ
   }
   */
  share(options) {
    // #ifdef H5
    let UA = navigator.appVersion;
    let uc = UA.split("UCBrowser/").length > 1 ? 1 : 0;
    let qq = UA.split("MQQBrowser/").length > 1 ? 2 : 0;
    let wx = ((UA.match(/MicroMessenger/i)) && (UA.match(/MicroMessenger/i).toString().toLowerCase() ==
      'micromessenger'));
    // 浏览器版本
    let qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
    let ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;
    let os = /iphone|ipod/i.test(navigator.userAgent) ? 1 : (/android/i.test(navigator.userAgent) ? 2 : 0);
    // qq浏览器下面 是否加载好了相应的api文件
    let qqBridgeDone = false;


    if (qq) {
      let type = '';
      //微信好友1, 微信朋友圈8,QQ 4
      if (options.type == 1) {
        type = 8;
      } else if (options.type == 2) {
        type = 1;
      } else if (options.type == 3) {
        type = 4;
      } else if (options.type == 4) {
        type = 11;
      } else if (options.type == 0) {
        type = 'undefined';
      }
      let share = function() {
        let shareInfo = {
          'url': options.url,
          'title': options.title,
          'description': options.desc,
          'img_url': options.img,
          'img_title': options.title,
          'to_app': type,
          'cus_txt': ''
        };
        if (window.browser) {
          browser.app && browser.app.share(shareInfo);
        } else if (window.qb) {
          qb.share && qb.share(shareInfo);
        }
      };
      if (qqBridgeDone) {
        share();
      } else {
        loadQQApi(qq, share);
      }

    }
    if (uc) {
      // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
      // 关于platform
      // ios: kWeixin || kWeixinFriend;
      // android: WechatFriends || WechatTimeline
      // uc 分享会直接使用截图
      let platform = ucSharePlatform(options.type || 0, os);
      let shareInfo = [options.title, options.desc, options.url, platform, '', '', ''];
      // android
      if (window.ucweb) {
        ucweb && ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
      }
      // ios
      else if (window.ucbrowser) {
        ucbrowser && ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
      }
    }
    // #endif

    // #ifdef APP-PLUS
    uni.getProvider({
      service: "share",
      success(res) {
        console.warn("------", res)
      }
    })
    // #endif
  }
}

export default share;
